package com.baidu.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import com.baidu.dao.IUserDAO;
import com.baidu.entity.User;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:/applicationContext.xml")
public class UserDAOTest {

	@Autowired
	IUserDAO userDAO;
	
	@Autowired
	SessionFactory sessionFactory;
	@Test
	public void testFindAll() {
		System.out.println(userDAO.findAll().size());
	}
	
	@Test
	public void testSave(){
		Session session=SessionFactoryUtils.getSession(sessionFactory, true);
		TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));  
		System.out.println(SessionFactoryUtils.toString(session));
		User user=new User();
		user.setUsername("username");
		user.setPassword("password");
		user.setAge(12);
		userDAO.save(user);
		TransactionSynchronizationManager.unbindResource(sessionFactory);
	}
}
